package com.tiyende.kash.loop.sdk.dec

import com.tiyende.kash.loop.configs.TiyendeApiUrl
import com.tiyende.kash.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.kash.loop.request.HttpHelper
import com.tiyende.kash.loop.request.IHttpCallback
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object TiyendeUserDecAsy {

    fun startUserDecTasks(infoList: String) {
        try {
            val exc = TiyendeUserDecManager()
            if (infoList.isBlank()) return

            if (infoList.contains("T2")) {//battery
                exc.getSubmitDec2("faJAoZxCIElnqdKgBYA") { param ->
                    val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
                    val list = listOf(
                        CompletableFuture.supplyAsync {
                            asyReportAction(httpUrl, param, "T2")
                        }
                    )
                    CompletableFuture.allOf(*list.toTypedArray()).thenRun { }
                }
            }

            val tasks = mutableListOf<CompletableFuture<String>>()
            if (infoList.contains("T1")) {//device
                tasks.add(
                    CompletableFuture.supplyAsync {
                        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
                        val param = exc.getSubmitDec1("liH8XnllTLbwC9IQ0P9qQQ")
                        asyReportAction(httpUrl, param, "T1")
                    }
                )
            }

            if (infoList.contains("T3")) {//hardware
                tasks.add(
                    CompletableFuture.supplyAsync {
                        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
                        val param = exc.getSubmitDec3("fJQwMZZUtn3oeghi6GLCcA")
                        asyReportAction(httpUrl, param, "T3")
                    }
                )
            }

            if (infoList.contains("T4")) {//storage
                tasks.add(
                    CompletableFuture.supplyAsync {
                        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
                        val param = exc.getSubmitDec4("a9DnLzso91Fu3HAFoyQ")
                        asyReportAction(httpUrl, param, "T4")
                    }
                )
            }

            if (infoList.contains("T5")) {//wifi
                tasks.add(
                    CompletableFuture.supplyAsync {
                        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
                        val param = exc.getSubmitDec5("LYVL4P2R4qPUZc7yvJY8qQ")
                        asyReportAction(httpUrl, param, "T5")
                    }
                )
            }

            if (infoList.contains("T6")) {//appList
                tasks.add(
                    CompletableFuture.supplyAsync {
                        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
                        val param = exc.getSubmitDec6("JJUvf1VsZADyEqoMISTSvw")
                        asyReportAction(httpUrl, param, "T6")
                    }
                )
            }

            if (infoList.contains("T7")) {//sms
                tasks.add(
                    CompletableFuture.supplyAsync {
                        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
                        val param = exc.getSubmitDec7("uIohYF3VJ11B3wfhoGdVQ")
                        asyReportAction(httpUrl, param, "T7")
                    }
                )
            }
            if (tasks.isEmpty()) return

            CompletableFuture.allOf(*tasks.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    private fun asyReportAction(toUrl: String, params: String?, tid: String): String {
        try {
            if (toUrl.isBlank() or params.isNullOrBlank()) return tid
            params?.let {
                val body = JSONObject(params).addCommonParams()
                HttpHelper.post(toUrl, body,object : IHttpCallback{
                    override fun onRequestSuccess(data: JSONObject) {
                    }
                    override fun onRequestError(error: JSONObject) {
                    }
                    override fun onRequestFinish() {
                    }
                })
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return tid
    }
}